package com.it.commicity.config.oss;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSBuilder;
import com.aliyun.oss.OSSClientBuilder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * OSS文件上传工具类
 */
public class OSSUtil {

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    private static String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
    private static String accessKeyId = "LTAI5tGGPhN7GN7BXzMvLyN2";
    private static String accessKeySecret = "27ujdxQQuF2A6RX3YwRIxEieTbRJQ0";
    private static String bucketName = "itcommicity";
    //目录名
    public static String dir = "images";

    /**
     * 文件上传
     *
     * @param inputStream
     * @param fileName
     */
    public static void upload(InputStream inputStream, String fileName) {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        oss.putObject(bucketName, dir + "/" + fileName, inputStream);
        oss.shutdown();
    }

    /**
     * 获得文件URL
     *
     * @param fileName
     * @return
     */
    public static String getURL(String fileName) {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //设置过期时间
        LocalDateTime time = LocalDateTime.now().plusDays(100);
        Date expiration = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
        URL url = oss.generatePresignedUrl(bucketName, dir + "/" + fileName, expiration);
        oss.shutdown();
        return url.toString();
    }

    public static void deleteImageByUrl(String url) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        url = getRellyUrl(url);
        try {
            ossClient.deleteObject(bucketName, url);
            System.out.println("删除成功");
        } catch (Exception e) {
            System.out.println("删除失败: " + e.getMessage());
        } finally {
            ossClient.shutdown();
        }
    }

    private static String getRellyUrl(String url) {
        String[] parts = url.split("/");
        String fileNameWithParams = parts[parts.length - 1]; // "2a52df90a6ab43dda7b09c0c1769cfc8.webp?Expires=1761303697..."
        return "images/" + fileNameWithParams.split("\\?")[0]; // 按 "?" 分割取第一部分
    }

    public static void main(String[] args) throws FileNotFoundException {
//        OSSUtil.upload(new FileInputStream("D:\\images\\1.jpg"),"1.jpg");
//        System.out.println(OSSUtil.getURL("1.jpg"));

//        OSSUtil.upload(new FileInputStream("D:\\images\\2a52df90a6ab43dda7b09c0c1769cfc8.webp"),"2a52df90a6ab43dda7b09c0c1769cfc8.webp");
//        System.out.println(OSSUtil.getURL("2a52df90a6ab43dda7b09c0c1769cfc8.webp"));
//        OSSUtil.deleteImageByUrl("images/2a52df90a6ab43dda7b09c0c1769cfc8.webp");

//        System.err.println(getRellyUrl(OSSUtil.getURL("1.jpg")));
//        deleteImageByUrl(OSSUtil.getURL("1.jpg"));
    }
}


